package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManMultiloansData;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManMultiloansRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 度小满多头信息
 *
 * @author renzhanzheng
 */

public class DuXiaoManMultiloansParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 度小满多头信息
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        DuXiaoManMultiloansRecord obj = (DuXiaoManMultiloansRecord)model;
        DuXiaoManMultiloansData data = obj.getData() == null ? new DuXiaoManMultiloansData() : obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject result = info.getJSONObject("result") == null ? new JSONObject() : info.getJSONObject("result");
        JSONObject mloanDetails = result.getJSONObject("mloan_details") == null ? new JSONObject() : result.getJSONObject("mloan_details");
        JSONObject queryTimes = mloanDetails.getJSONObject("query_times") == null ? new JSONObject() : mloanDetails.getJSONObject("query_times");
        JSONObject queryOrgCnt = mloanDetails.getJSONObject("query_org_cnt") == null ? new JSONObject() : mloanDetails.getJSONObject("query_org_cnt");
        JSONObject queryOrgTypes = mloanDetails.getJSONObject("query_org_types") == null ? new JSONObject() : mloanDetails.getJSONObject("query_org_types");
        JSONObject d30 = queryOrgTypes.getJSONObject("30d") == null ? new JSONObject() : queryOrgTypes.getJSONObject("30d");
        JSONObject m2 = queryOrgTypes.getJSONObject("2m") == null ? new JSONObject() : queryOrgTypes.getJSONObject("2m");
        JSONObject m3 = queryOrgTypes.getJSONObject("3m") == null ? new JSONObject() : queryOrgTypes.getJSONObject("3m");
        JSONObject m6 = queryOrgTypes.getJSONObject("6m") == null ? new JSONObject() : queryOrgTypes.getJSONObject("6m");
        JSONObject m8 = queryOrgTypes.getJSONObject("8m") == null ? new JSONObject() : queryOrgTypes.getJSONObject("8m");

        StringBuilder sb = getBaseInfo(obj, successful);

        //错误码
        sb.append(info.getString("retCode") == null ? "" : info.getString("retCode")).append(CRT001);
        //错误话术
        sb.append(info.getString("retMsg") == null ? "" : info.getString("retMsg")).append(CRT001);
        //多头分
        sb.append(result.getString("score") == null ? "" : result.getString("score")).append(CRT001);
        //0-30天查询次数
        sb.append(queryTimes.getString("30d") == null ? "" : queryTimes.getString("30d")).append(CRT001);
        //0-60天查询次数
        sb.append(queryTimes.getString("2m") == null ? "" : queryTimes.getString("2m")).append(CRT001);
        //0-90天查询次数
        sb.append(queryTimes.getString("3m") == null ? "" : queryTimes.getString("3m")).append(CRT001);
        //0-180天查询次数
        sb.append(queryTimes.getString("6m") == null ? "" : queryTimes.getString("6m")).append(CRT001);
        //0-240天查询次数
        sb.append(queryTimes.getString("8m") == null ? "" : queryTimes.getString("8m")).append(CRT001);
        //0-30天查询机构数
        sb.append(queryOrgCnt.getString("30d") == null ? "" : queryOrgCnt.getString("30d")).append(CRT001);
        //0-60天查询机构数
        sb.append(queryOrgCnt.getString("2m") == null ? "" : queryOrgCnt.getString("2m")).append(CRT001);
        //0-90天查询机构数
        sb.append(queryOrgCnt.getString("3m") == null ? "" : queryOrgCnt.getString("3m")).append(CRT001);
        //0-180天查询机构数
        sb.append(queryOrgCnt.getString("6m") == null ? "" : queryOrgCnt.getString("6m")).append(CRT001);
        //0-240天查询机构数
        sb.append(queryOrgCnt.getString("8m") == null ? "" : queryOrgCnt.getString("8m")).append(CRT001);
        //0-30天p2p类机构查询次数
        sb.append(d30.getString("p2p") == null ? "" : d30.getString("p2p")).append(CRT001);
        //0-30天消费分期类机构查询次数
        sb.append(d30.getString("xffq") == null ? "" : d30.getString("xffq")).append(CRT001);
        //0-30天现金贷类机构查询次数
        sb.append(d30.getString("xjd") == null ? "" : d30.getString("xjd")).append(CRT001);
        //0-30天其他类机构查询次数
        sb.append(d30.getString("other") == null ? "" : d30.getString("other")).append(CRT001);
        //0-60天p2p类机构查询次数
        sb.append(m2.getString("p2p") == null ? "" : m2.getString("p2p")).append(CRT001);
        //0-60天消费分期类机构查询次数
        sb.append(m2.getString("xffq") == null ? "" : m2.getString("xffq")).append(CRT001);
        //0-60天现金贷类机构查询次数
        sb.append(m2.getString("xjd") == null ? "" : m2.getString("xjd")).append(CRT001);
        //0-60天其他类机构查询次数
        sb.append(m2.getString("other") == null ? "" : m2.getString("other")).append(CRT001);
        //0-90天p2p类机构查询次数
        sb.append(m3.getString("p2p") == null ? "" : m3.getString("p2p")).append(CRT001);
        //0-90天消费分期类机构查询次数
        sb.append(m3.getString("xffq") == null ? "" : m3.getString("xffq")).append(CRT001);
        //0-90天现金贷类机构查询次数
        sb.append(m3.getString("xjd") == null ? "" : m3.getString("xjd")).append(CRT001);
        //0-90天其他类机构查询次数
        sb.append(m3.getString("other") == null ? "" : m3.getString("other")).append(CRT001);
        //0-180天p2p类机构查询次数
        sb.append(m6.getString("p2p") == null ? "" : m6.getString("p2p")).append(CRT001);
        //0-180天消费分期类机构查询次数
        sb.append(m6.getString("xffq") == null ? "" : m6.getString("xffq")).append(CRT001);
        //0-180天现金贷类机构查询次数
        sb.append(m6.getString("xjd") == null ? "" : m6.getString("xjd")).append(CRT001);
        //0-180天其他类机构查询次数
        sb.append(m6.getString("other") == null ? "" : m6.getString("other")).append(CRT001);
        //0-240天p2p类机构查询次数
        sb.append(m8.getString("p2p") == null ? "" : m8.getString("p2p")).append(CRT001);
        //0-240天消费分期类机构查询次数
        sb.append(m8.getString("xffq") == null ? "" : m8.getString("xffq")).append(CRT001);
        //0-240天现金贷类机构查询次数
        sb.append(m8.getString("xjd") == null ? "" : m8.getString("xjd")).append(CRT001);
        //0-240天其他类机构查询次数
        sb.append(m8.getString("other") == null ? "" : m8.getString("other")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n","").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_duxiaoman_multiloans_inc";
    }

    @Override
    public String getType() {
        return RecordType.DUXIAOMAN_MULTILOANS_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "度小满多头信息";
    }
}
